<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    /**
    * Main class for working with site pages named "posts".
    * @author NU SIT Bar
    * @version 1.0
    * @created 17.Jul.2012 17:39:44 h
    */

    class Albums extends MX_Controller 
    {

        private $data;

        function __construct()
        {

            parent::__construct();
            if(!$this->session->userdata('logged_in')){
                redirect(base_url());    
            }
            $this->load->config('pages');

        }

        public function index()
        { 
            if(!isset($_GET['post_type']))
            {
                $_GET['post_type']="page";
            }
            $this->view_all();
        }

        /**
        * 
        * @param $_GET['post_type']
        * @param $_GET['type']
        */
        function view_all()
        {   
            if(!isset($_GET['type']))
            {
                $_GET['type']="page";
            }
            $this->data['page'] = 'Gallery';
            $this->data['title'] = 'View all Galleries';
            $this->template->build('albums/view_all',$this->data);
        }

        function view_new()
        {  
            $this->data['page'] = 'Gallery';
            $this->data['title'] = 'Add Gallery';
            $this->template->build('albums/add_album_images', $this->data);        
        }

        /**
        * 
        * @param $_GET['post_ID']
        * @param $_GET['post_type']
        */
        function view_edit()
        {      
            $this->data['post_action'] = "EDIT";          
            $this->data['page'] = 'Gallery';
            $this->data['title'] = 'Edit Gallery';
            $this->template->build('albums/edit_album', $this->data);
        }

        /**
        * 
        * new-gallery
        * 
        * @param $_POST['post_action']
        * @param $_POST['type']
        * @param $_POST['galleryname']
        * @param $_POST['parent_id']
        * 
        *  edit-gallery
        * 
        * @param $_POST['post_action']
        * @param $_POST['gallery_ID']
        * @param $_POST['linkto']
        * @param $_POST['galleryname']
        * @param $_POST['parent_id']
        * 
        * new-pictures
        * 
        * @param $_POST['gallery_ID']
        * 
        */        
        function submit_gallery(){

            /** module run gallery submit_post **/
            switch($_POST['post_action']){

                case 'new-gallery':     $result = modules::run('gallery/add_gallery');
                    break;

                case 'edit-gallery':    $result = modules::run('gallery/edit_gallery');
                    break;

                case 'new-pictures':    
                    $result = modules::run('gallery/add_pictures');
                    $result = explode('-',$result);

                    // Return
                    if($result[0]){
                        redirect(base_url('albums/view_all?post_type=gallery&type=all'));
                    }else{
                        show_error('Something went wrong. Add picture fails.');
                    } 
                    break;

                case 'new-videos':    $this->gallery_model->create_videos();
                    break;

            }   

            $result = explode('-',$result);

            // Return
            if($result[0]){
                echo json_encode(array('action'=>true,'msg'=>$result[1]));
            }else{
                echo json_encode(array('action'=>true,'msg'=>'Something went wrong. Please try again.'));
            }   

        }

        function delete($ID){

            /** module run page submit_post **/
            $result = modules::run('gallery/delete',array('gallery_ID'=>$ID));

            if($result){
                redirect(base_url('albums/view_all?post_type=gallery&type=all'));
            }else{
                show_error('Error ocured while deleting gallery!');
            }

        }

        function delete_picture(){

            $result = modules::run('gallery/delete_picture');

            // Return
            if($result[0]){
                redirect(base_url('albums/view_edit?gallery_ID='.$_GET['gallery_ID'].'&post_type=gallery'));
            }else{
                show_error('Something went wrong. Delete picture fails.');
            }

        }    

        function picture_order()
        {

            $result = modules::run('gallery/picture_order');

            // Return
            if($result[0]){
                redirect(base_url('albums/view_edit?gallery_ID='.$_POST['gallery_ID'].'&post_type=gallery'));
            }else{
                show_error('Something went wrong. Order pictures fails.');
            }    
        }

    }

    /* End of file pages.php */
    /* Location: ./application/modules/pages/controllers/pages.php */